angular.module('imsApp').controller('creativesController', ['$scope', '$rootScope', '$state', 'CreativesManager', 'ImsDialog', function ($scope, $rootScope, $state, CreativesManager, ImsDialog) {
    $scope.groupId = $state.params.groupId;
    $scope.campaignsId = $state.params.campaignsId;
    $scope.search = {
        pageNo: $state.params.pageNo || 1,
        pageSize: $state.params.pageSize || 10,
        groupId: $state.params.groupId,
        campaignsId:$state.params.campaignsId
    };
    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, {
                groupId: $scope.search.groupId,
                pageNo: $scope.search.pageNo,
                pageSize: $scope.search.pageSize,
            })
        }
    };
    CreativesManager.creativesList($scope.search.groupId, [$scope.search.pageNo, $scope.search.pageSize]).then(function (result) {
        $scope.lists = result.data.pageList;
        $scope.paginationConf.totalItems = result.data.total;

        $scope.title = result.data.title;
    })

    $scope.changeStatus = function (item) {
        var status = "";
        if (item.status == 3) {
            status = 4
        } else {
            status = 3
        }
        CreativesManager.creativesStatusEdit(item.creativeId, {status: status}).then(function (result) {
            console.log(result)
            $state.reload();

        })
    }

    $scope.deletes = function (key) {
        $scope.search.pageNo=1;
        $scope.search[key]=null;
        $state.go($state.current.name, $scope.search)

    }

    $scope.deleteCreatives=function () {
        var ids=[];
        $scope.lists.forEach(function (creative) {
            if(creative.checked){
                ids.push(creative.creativeId)
            }
        })
        if(!ids.length){
            return false;
        }else{
            ImsDialog.open({title: "删除确认", message: "您确定要删除数据吗?"}).then(function (a) {
                if (a == 'ok') {
                    CreativesManager.creativesDelete(ids).then(function (result) {
                        console.log(result,"fff");
                        $state.reload();
                    })
                }
            })
        }
    }

}])

